﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Data;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ScrumTable.Common.Data;
using ScrumTable.Common.Excel.OpenXmlSdk;
using ScrumTable.Common.Excel.OpenXmlSdk.Extensions;

#endregion 

namespace ScrumTable.Common.Excel.Test
{
    /// <summary>
    /// This is the test class for Row class.
    /// </summary>
    [TestClass]
    public class RowTest
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private DataRow ToTest { get; set; }
        private DataSet DataContext { get; set; }

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        #endregion

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new RowTest instance.
        /// </summary>
        public RowTest()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Use TestInitialize to run code before running each test .
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            IWorkbookSerializer workbook = new OpenXmlWorkbookSerializer(Resources.GetProjectTestDataFilePath(TestContext.TestDir));
            DataContext = workbook.Deserialize();

            DataTable secondSheet = DataContext.Tables[Resources.SecondWorksheetName];
            ToTest = secondSheet.GetRows().First();
        }

        [TestMethod]
        public void TestGetValue()
        {
            Assert.AreEqual(new DateTime(2011, 11, 11), ToTest.GetColumnValue<DateTime>(Resources.SecondSheetStartDateFieldName));
            Assert.AreEqual(new DateTime(2011, 11, 30), ToTest.GetColumnValue<DateTime>(Resources.SecondSheetEndDateFieldName));
        }

        [TestMethod]
        public void TestGetValueWithErrorAndDefaultFallback()
        {
            DateTime left = ToTest.GetColumnValue(Resources.SecondSheetNameFieldName, DateTime.Now);
            DateTime right = DateTime.Now;

            Assert.IsTrue(left <= right);
        }

        [TestMethod]
        public void TestAccommodatesValues()
        {
            DataRow element1004;
            DataContext.Tables[Resources.ThirdWorksheetName].TryGetRowById(1004, out element1004);
            Assert.IsTrue(element1004.AccommodatesValues());
        }

        [TestMethod]
        public void TestGetPrimaryKeyValues()
        {
            DataRow element1004;
            DataContext.Tables[Resources.ThirdWorksheetName].TryGetRowById(1004, out element1004);
            Assert.AreEqual(1004, element1004.GetFirstPrimaryKey<int>());
        }

        [TestMethod]
        public void TestGetRows()
        {
            Assert.IsNotNull(DataContext.Tables[Resources.ThirdWorksheetName].GetRows());
            Assert.AreEqual(5, ToTest.Table.GetColumns().Count());
        }

        [TestMethod]
        public void TestGetColumns()
        {
            Assert.IsNotNull(DataContext.Tables[Resources.ThirdWorksheetName].GetColumns());
            Assert.AreEqual(Resources.ThirdSheetColumnCount, DataContext.Tables[Resources.ThirdWorksheetName].GetColumns().Count());
        }


        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}